package com.framework.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.SysCardDao;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.sys.SysAgentInfo;
import com.framework.entity.sys.SysCard;
import com.framework.exception.RRException;
import com.framework.exception.menu.RespCode;
import com.framework.service.AgentInfoService;
import com.framework.service.AgentService;
import com.framework.service.SysCardService;
import com.framework.utils.jiufu.ShopUtil;
import com.framework.utils.string.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("sysCardService")
public class SysCardServiceImpl extends ServiceImpl<SysCardDao, SysCard> implements SysCardService {

	@Autowired
	private AgentService agentService;
	@Autowired
	private AgentInfoService agentInfoService;

	@Override
	public List<Map<String,Object>> queryCardList(String token,String cardState,String parms,Integer pageNo) {
		if(pageNo == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		pageNo = pageNo * 10;
		SysAgent agent = agentService.queryAgentByToken(token);
		return baseMapper.queryCardList(agent.getAgentId(), cardState, parms, pageNo);
	}

	@Override
	public Map<String, Object> queryCardRewardMap(String token, String monthTime) {
		if(StringUtils.isBlank(monthTime)){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		return baseMapper.queryCardRewardMap(agentId,monthTime);
	}

	@Override
	public void insertCard(SysCard sysCard) {
		//参数校验
		if(sysCard == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		if(StringUtils.isBlank(sysCard.getBankType()) ||
				StringUtils.isBlank(sysCard.getProName()) ||
				StringUtils.isBlank(sysCard.getBankType()) ||
				StringUtils.isBlank(sysCard.getSfzId()) ||
				StringUtils.isBlank(sysCard.getPhone()) ||
				sysCard.getAgentId() == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		SysAgent agent = agentService.queryAgentById(sysCard.getAgentId());
		if(agent == null){
			throw new RRException("9000","代理商id错误");
		}
		Long agentId = agent.getAgentId();
		//校验是否可以申请信用卡
		boolean isCard = queryIsCardRepeat(agentId,sysCard.getBankType(),sysCard.getPhone());
		if(!isCard){
			throw new RRException("9000","不能在同一代理商下重复申请信用卡");
		}
		sysCard.setProDate(new Date());
		sysCard.setCreateTime(new Date());
		sysCard.setAgentId(agentId);
		baseMapper.insert(sysCard);
	}

	@Override
	public boolean queryIsCardRepeat(Long agentId, String bankType, String phone) {
		if(agentId == null ||
				StringUtils.isBlank(bankType) ||
				StringUtils.isBlank(phone)){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		SysAgent agent = agentService.queryAgentById(agentId);
		if(agent == null){
			throw new RRException("9000","代理商id错误");
	}
		return baseMapper.queryIsCardRepeat(agentId,bankType,phone);
	}

	@Override
	public String queryShopId(Long agentId) {
		if(agentId == null){
			throw new RRException(RespCode.NOT_PARAM_ERROR);
		}
		SysAgent agent = agentService.queryAgentById(agentId);
		SysAgentInfo agentInfo = agentInfoService.getAgentInfoById(agentId);
		if(agent == null || agentInfo == null){
			throw new RRException("9000","代理商id错误");
		}
		String shopId = agentInfo.getShopId();
		String phone = agent.getPhone();
		//没有id需要生成玖富贷款id
		if(StringUtils.isBlank(shopId)){
			ShopUtil.getShopId(phone);
		}
		return shopId;
	}

}
